import navigator from "../../module/navigator";
import sessionModule from "../../module/sessionStorage";
// import Router from '@/router'

export default {
  // 设置导航数据（后端数据格式话，返回原数据经过格式化后的数组，未经过树处理）
  setNavigatorMenuOriginData(state, data) {
    state.navigatorMenuList = data;
    sessionModule.save({
      key: "TardeMenusData",
      value: JSON.stringify(data),
    });
  },
  // 新增菜单
  addMenuBar(state, form) {
    let originForm = JSON.parse(JSON.stringify(form));
    // 格式化数据
    let data = navigator.addMenuForm(originForm);
    // Push到Vuex
    state.navigatorMenuList.push(data);
  },
  // 更改选中菜单的ID- 专业版
  changeNavigatorCurrentId(state, data) {
    let { id, ifMenuBar } = data;
    if (ifMenuBar) {
      let list = state.navigatorMenuList;
      let index = state.navigatorMenuList.findIndex((e) => e.meta.id == id);
      let menu = state.navigatorMenuList.find((e) => e.meta.id == id);
      let childrenDevelop = menu.meta.display.childrenDevelop;
      list[index].meta.display.childrenDevelop = !childrenDevelop;
    } else {
      state.currentNavigatorId = id;
    }
  },
  changeIconNavigatorCurrentId(state, data) {
    let { id } = data;
    state.currentIconNavigatorId = id;
  },
  changeHeadersOptionsSort(state, data) {
    let { newSort } = data;
    let newList = newSort.map((id) => {
      return state.layout.header.options.list.find((item) => item.id == id);
    });
    state.layout.header.options.list = [...newList];
  },
  changeHeadersOptionsStatus(state, data) {
    let { id, disable } = data;

    let newList = state.layout.header.options.list.map((item) => {
      if (item.id == id && item.sureDisable) {
        item.disable = disable;
      }
      return item;
    });
    state.layout.header.options.list = [...newList];
  },
  // 更改主题
  updateCurrentTheme(state, data) {
    state.theme.current = data.current;
  },
  // 更改侧边栏收缩（显示）状态
  changeNavigatorShow(state, data) {
    let { newStatus } = data;
    state.navigatorShow = newStatus;
  },
  // 更改导航菜单Item 元信息
  changeNavigatorItemMeta(state, data) {
    let { id, meta } = data;
    let index = state.navigatorMenuList.findIndex((item) => item.id == id);
    let OriginMeta = state.navigatorMenuList[index].meta;
    Object.assign(OriginMeta, meta);
  },
  // 更新配置数据
  updateSetting(state, data) {
    let {
      // type, // 更新类型
      key, // 设置组
      setting, // 设置组-子组
      value, // 更新内容
    } = data;

    let stateSetting = state.setting;

    // 是否允许修改
    let globalRevise = stateSetting.update;

    let settingList = stateSetting.list.find((item) => item.key == key);

    if (globalRevise) {
      let { operate } = settingList;

      let groupRevise = operate.revise;
      if (groupRevise) {
        let listItem = settingList.setting.find((i) => i.key == setting);

        let itemRevise = listItem.operate.revise;

        if (itemRevise) {
          console.log("listItem");
          console.log(listItem);
          listItem.value = value;
        } else {
          console.log({
            code: "c314,error,settingItem 禁止修改",
          });
        }
      } else {
        console.log({
          code: "c314,error,groupRevise 禁止修改",
        });
      }
    } else {
      console.log({
        code: "c314,error,globalRevise 禁止修改",
      });
    }
  },
};
